<?php
/**
 *  MyApp_ViewClass_Base.php
 *
 *  @author
 *  @package    Smscom
 *  @version
 */

require_once 'MyApp_SmartyPlugin.php';

/**
 *  viewクラス
 *
 *  @author
 *  @package    Smscom
 *  @access     public
 */
class MyApp_ViewClass_Base extends Ethna_ViewClass {
    /**
     * Smarty
     *
     * @var object
     */
    var $smarty;
    
    /**
     *
     * @var string
     */
    var $layout = null;

    /**
     *
     *  @access protected
     *  @param  object  MyApp_Renderer
     */
    function _setDefault(&$renderer)
    {
        $this->_setSmartyPlugins($renderer);
        
        $this->smarty = &$renderer->getEngine();

        $this->smarty->left_delimiter = '<%';
        $this->smarty->right_delimiter = '%>';

        $this->smarty->compile_check = true;

        $this->smarty->plugins_dir[] = './plugins_ex/';
        
        $c =& Ethna_Controller::getInstance();
        $config =& $c->getConfig();
        $site_url = $config->get('base_url');
        $this->smarty->assign('site_url', $site_url);
        
        if (!empty($this->af->set_csrf_id)) {
            Ethna_Util::setCsrfID();
        }
    }
    
    /**
     *  Smarty
     *
     *  @access protected
     *  @param  object  Smscom_Renderer
     */
    function _setSmartyPlugins($renderer)
    {
        $renderer->setPlugin('url','function','smarty_function_url_ex');
        $renderer->setPlugin('form','block','smarty_block_form_ex');
        $renderer->setPlugin('form_input','function','smarty_function_form_input_ex');
        
        $renderer->setPlugin('istyle','function','smarty_function_istyle');
        $renderer->setPlugin('form_value','function','smarty_function_form_value');
        $renderer->setPlugin('thumb','modifier','smarty_modifier_thumb');
        $renderer->setPlugin('emoji2img','modifier','smarty_modifier_emoji2img');
        $renderer->setPlugin('emoji_strip','modifier','smarty_modifier_emoji_strip');
        $renderer->setPlugin('convert_external_link','modifier','smarty_modifier_convert_external_link');
        $renderer->setPlugin('replace_session_tag','modifier','smarty_modifier_replace_session_tag');
        $renderer->setPlugin('message','function','smarty_function_message2');
    }
    
    /**
     *  @access protected
     *  @param  string
     */
    function _setSearchBox($fullsearch_category_key)
    {
        
    }
    
    /**
     *
     *  @access protected
     */
    function _setNewFeatureList()
    {
        
    }
    
    /**
     *
     *  @access protected
     */
    function _setFavoriteBbsList()
    {
        
    }
    
    /**
     *  @access protected
     */
    function _setFavoriteBlogList()
    {
        
    }
    
    /**
     *
     *  @access public
     */
    function forward()
    {
        $renderer =& $this->_getRenderer();
        $this->_setDefault($renderer);
        
        if (preg_match('/^https?:\/\//', $this->forward_name)) {
            $this->redirect($this->forward_name);
        }
        
        if (!empty($this->layout)) {
            if ((is_absolute_path($this->layout) && is_readable($this->layout)) || is_readable($renderer->template_dir . $this->layout)) {
                $renderer->assign('content_for_layout', $renderer->perform($this->forward_path, true));
                $renderer->perform($this->layout);
            } else {
                return Ethna::raiseWarning('file "' . $this->layout . '" not found');
            }
        } else {
            $renderer->perform($this->forward_path);
        }
    }
    
    /**
     *
     * @param string $url
     */
    function redirect($url)
    {
        header('Location: '. $url);
        exit();
    }
    
    /**
     *  (type="form")
     * 
     *  CSRF
     *
     *  @access protected
     */
    function getFormBlock($content, $params)
    {
        if (isset($params['method']) === false) {
            $params['method'] = 'post';
        }
        if (!empty($this->af->set_csrf_id)) {
            $csrf = smarty_function_csrfid(array(), $renderer);
            $content .= $csrf;
        }
        
        return $this->_getFormInput_Html('form', $params, $content, false);
    }
    
    /**
     *(type="text")
     *
     *  @access protected
     */
    function _getFormInput_Text($name, $def, $params)
    {
        $params['type'] = 'text';

        if (isset($def['type'])) {
            $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
        } else {
            $params['name'] = $name;
        }

        $value = '';
        if (isset($params['value'])) {
            $value = $params['value'];
        } else if (isset($params['default'])) {
            $value = $params['default'];
        } else if (isset($def['default'])) {
            $value = $def['default'];
        }
        if (is_array($value)) {
            if ($def['_form_counter'] < count($value)) {
                $params['value'] = $value[$def['_form_counter']];
            } else {
                $params['value'] = '';
            }
        } else {
            $params['value'] = $value;
        }

        if (isset($def['max']) && $def['max']) {
            $params['maxlength'] = $def['max'];
            
            if (Mobile_Util::isDoCoMo()) {
                if (!isset($def['istyle']) || $def['istyle'] == 1) {
                    $params['maxlength'] = $def['max'] * 2;
                }
            }
        }
        
        if (isset($def['istyle']) && $def['istyle']) {
            $istyle_attr = Mobile_Util::getIStyle($def['istyle'], false);
            $params = array_merge($params, $istyle_attr);
        }

        return $this->_getFormInput_Html('input', $params);
    }
    
    /**
     *  textarea
     *
     *  @access protected
     */
    function _getFormInput_Textarea($name, $def, $params)
    {
        if (isset($def['type'])) {
            $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
        } else {
            $params['name'] = $name;
        }

        $element = '';
        if (isset($params['value'])) {
            $element = $params['value'];
            unset($params['value']);
        } else if (isset($params['default'])) {
            $element = $params['default'];
        } else if (isset($def['default'])) {
            $element = $def['default'];
        }
        if (is_array($element)) {
            if ($def['_form_counter'] < count($element)) {
                $element = $element[$def['_form_counter']];
            } else {
                $element = '';
            }
        } else {
            $params['value'] = $element;
        }
        
        if (isset($def['istyle']) && $def['istyle']) {
            $istyle_attr = Mobile_Util::getIStyle($def['istyle'], false);
            $params = array_merge($params, $istyle_attr);
        }

        return $this->_getFormInput_Html('textarea', $params, $element);
    }
    
    /**
     * (type="password")
     *
     *  @access protected
     */
    function _getFormInput_Password($name, $def, $params)
    {
        $params['type'] = 'password';
        if (isset($def['type'])) {
            $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
        } else {
            $params['name'] = $name;
        }

        $value = '';
        if (isset($params['value'])) {
            $value = $params['value'];
        } else if (isset($params['default'])) {
            $value = $params['default'];
        } else if (isset($def['default'])) {
            $value = $def['default'];
        }
        if (is_array($value)) {
            if ($def['_form_counter'] < count($value)) {
                $params['value'] = $value[$def['_form_counter']];
            } else {
                $params['value'] = '';
            }
        } else {
            $params['value'] = $value;
        }

        if (isset($def['max']) && $def['max']) {
            $params['maxlength'] = $def['max'];
            
            if (Mobile_Util::isDoCoMo()) {
                if (!isset($def['istyle']) || $def['istyle'] == 1) {
                    $params['maxlength'] = $def['max'] * 2;
                }
            }
        }
        
        if (isset($def['istyle']) && $def['istyle']) {
            $istyle_attr = Mobile_Util::getIStyle($def['istyle'], false);
            $params = array_merge($params, $istyle_attr);
        }

        return $this->_getFormInput_Html('input', $params);
    }
    
    /**
     * (type="select")
     *
     *  @access protected
     */
    function _getFormInput_Select($name, $def, $params)
    {
        if (isset($def['type'])) {
            $params['name'] = is_array($def['type']) ? $name . '[]' : $name;
        } else {
            $params['name'] = $name;
        }

        if (isset($def['option']) && is_array($def['option'])) {
            $options = $def['option'];
        } else {
            $options = array();
        }

        if (isset($params['default'])) {
            $current_value = $params['default'];
        } else if (isset($def['default'])) {
            $current_value = $def['default'];
        } else {
            $current_value = array();
        }
        $current_value = array_map('strval', to_array($current_value));

        if (isset($params['separator'])) {
            $separator = $params['separator'];
        } else {
            $separator = "\n";
        }

        $contents = array();
        $selected = false;
        foreach ($options as $key => $value) {
            $attr = array('value' => $key);
            if (in_array((string) $key, $current_value, true)) {
                $attr['selected'] = 'selected';
                $selected = true;
            }
            $contents[] = $this->_getFormInput_Html('option', $attr, $value);
        }

        if (isset($params['emptyoption'])) {
            $attr = array('value' => '');
            if ($selected === false) {
                $attr['selected'] = 'selected';
            }
            array_unshift($contents,
                          $this->_getFormInput_Html('option',
                                                    $attr,
                                                    $params['emptyoption']));
            unset($params['emptyoption']);
        }

        $element = $separator . implode($separator, $contents) . $separator;
        $select_tag =  $this->_getFormInput_Html('select', $params, $element, false);
        
        if (Mobile_Util::isEZweb()) {
            $font_attr = array('size' => 3);
            $select_tag = $this->_getFormInput_Html('font', $font_attr, $select_tag, false);
        }
        
        return $select_tag;
    }
    
    /**
     *  @access public
     */
    function getFormValue($name, $action, $params)
    {
        $af =& $this->_getHelperActionForm($action, $name);
        if ($af === null) {
            return '';
        }
        
        $value = $af->get($name);
        
        $def = $af->getDef($name);
        if ($def === null || $value == null) {
            return '';
        }
        
        if (isset($def['form_type']) === false) {
            $def['form_type'] = FORM_TYPE_TEXT;
        }
        
        if (isset($params['separator'])) {
            $separator = $params['separator'];
        } else {
            $separator = ", ";
        }
        
        switch ($def['form_type']) {
        case FORM_TYPE_CHECKBOX:
            $option = $this->_getSelectorOptions($af, $def, $params);
            if (!is_array($value)) {
                $value = $option[$value];
            } else {
                $ret = array();
                foreach ($value as $val) {
                    $ret[] = $option[$val];
                }
                $value = implode($separator, $ret);
            }
            break;
            
        case FORM_TYPE_RADIO:
            $option = $this->_getSelectorOptions($af, $def, $params);
            if (!is_array($value)) {
                $value = $option[$value];
            } else if (count($value)) {
                $value = $option[$value[0]];
            }
            break;
            
        case FORM_TYPE_SELECT:
            $option = $this->_getSelectorOptions($af, $def, $params);
            if (!is_array($value)) {
                $value = $option[$value];
            } else if (count($value)) {
                $value = $option[$value[0]];
            }
            break;
        }
        
        return $value;
    }
}
?>
